package jeecg.business.controller.enter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.service.enter.EnterServiceI;
import jeecg.system.pojo.base.TSUser;
import jeecg.system.service.SystemService;
import jeecg.system.service.UserService;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.ContextHolderUtils;
import org.jeecgframework.core.util.FileUtils;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

/**   
 * @Title: Controller
 * @Description: 企业用户
 * @author LinYingPiaoJian
 * @date 2015-06-04 17:55:00
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/enterController")
public class EnterController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(EnterController.class);

	@Autowired
	private EnterServiceI enterService;
	@Autowired
	private SystemService systemService;
	@Autowired
	UserService userService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 企业用户列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "toenterIndex")
	public ModelAndView toenterIndex(HttpServletRequest request){
		String id = request.getParameter("id");
		EnterEntity enter = enterService.get(EnterEntity.class, id);
		request.setAttribute("forward", "outController.do?index");
		HttpSession session = ContextHolderUtils.getSession();
		session.setAttribute("enter", enter);
		
		return new ModelAndView("jeecg/business/out/forward");
	}

	
	/**
	 * 企业用户列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "enter")
	public ModelAndView enter(HttpServletRequest request) {
		return new ModelAndView("jeecg/business/enter/enterList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */
	@RequestMapping(params = "datagrid")
	public void datagrid(EnterEntity enter,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(EnterEntity.class, dataGrid);
		//查询条件组装器
		if(enter != null){
			if(StringUtil.isNotEmpty(enter.getAccount())){
				enter.setAccount("*"+enter.getAccount()+"*");
			}
			if(StringUtil.isNotEmpty(enter.getName())){
				enter.setName("*"+enter.getName()+"*");
			}
		}
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, enter);
		this.enterService.getDataGridReturn(cq, true);
		
		List<EnterEntity> list = cq.getDataGrid().getReaults();
		if(list != null){
			for(EnterEntity each : list){
				each.setName("<span onClick=openNewWindow(\"enterController.do?toenterIndex&id="+each.getId()+"\"); class=\"nms_base_title\">"+each.getName()+"</span>");	
			}
			cq.setReaults(list);
		}
		
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除企业用户
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(EnterEntity enter, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		enter = systemService.getEntity(EnterEntity.class, enter.getId());
		message = "删除成功";
		enterService.deleteEnter(enter);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加企业用户
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(EnterEntity enter, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(enter.getId())) {
			message = "更新成功";
			EnterEntity t = enterService.get(EnterEntity.class, enter.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(enter, t);
				enterService.updateEnter(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			message = "添加成功";
			enterService.saveEnter(enter);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		
		return j;
	}

	
	@RequestMapping(params = "resetpwd")
	public ModelAndView resetpwd(EnterEntity enter, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(enter.getId())) {
			enter = enterService.getEntity(EnterEntity.class, enter.getId());
			TSUser user = userService.queryUserByName(enter.getAccount());
			req.setAttribute("user", user);
		}
		return new ModelAndView("system/user/resetpwd");
	}
	
	/**
	 * 企业用户列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(EnterEntity enter, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(enter.getId())) {
			enter = enterService.getEntity(EnterEntity.class, enter.getId());
		}else{
			if(enter == null) enter = new EnterEntity();
		}
		if(StringUtil.isEmpty(enter.getZxpl())) enter.setZxpl("0 0 0/1 * * ?");
		req.setAttribute("enterPage", enter);
		req.setAttribute("sflist", HtmlParseResult.sfList);
		return new ModelAndView("jeecg/business/enter/enter");
	}
	
	@RequestMapping(params = "uploadpic" ,method = RequestMethod.GET)
	public ModelAndView uploadpic(EnterEntity enter, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(enter.getId())) {
			enter = enterService.getEntity(EnterEntity.class,enter.getId());
			req.setAttribute("enter", enter);
		}
		req.setAttribute("type", req.getParameter("type"));
		return new ModelAndView("jeecg/business/enter/uploadpic");
	}
	
	String uploadFilePath = "uploadfile";
	@RequestMapping(params = "savePic", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson savePic(MultipartHttpServletRequest request) throws Exception {
		AjaxJson j = new AjaxJson();				
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		EnterEntity enter = enterService.getEntity(EnterEntity.class, id);
		Map<String, MultipartFile> fileMap = request.getFileMap();	
		String uuid = UUID.randomUUID().toString();
		
		String realPath = request.getSession().getServletContext().getRealPath("/") + uploadFilePath+"/";// 文件的硬盘真实路径
		String oldname = null;
		String extend = null;
		for (MultipartFile file : fileMap.values()) {
			String outpath = systemService.uploadFile(uuid, realPath, file);
			oldname = file.getOriginalFilename();
			extend = FileUtils.getExtend(file.getOriginalFilename());
		}
		String viewFilePath = uploadFilePath+"/"+uuid+"."+extend;
		message = "上传成功";
		j.setMsg(message);
		if("top".equals(type)){
			enter.setTopphoto(viewFilePath);
		}else{
			enter.setBottomphoto(viewFilePath);
		}
		enterService.updateEntitie(enter);
		return j;
	}
	
	@RequestMapping(params = "getOneEnter")
	public String getOneEnter() {
		return "jeecg/business/enter/getOneEnterList";
	}
	
	
}
